<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>网络知识（2）--Nginx | 小小错题本</title>
    <meta name="description" content="
1.1 正向代理
&gt; 正向代理，意思是一个位于客户端和原始服务器(origin server)之间的服务器，为了从原始服务器取得内容，客户端向代理发送一个请求并指定目标(原始服务器)，然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。

&gt; 举个例子：用户想访问www.google.com，不能直接访问。需要先访问代理服务器A，A服务器会去访问www.go ...">
    <meta name="generator" content="VuePress 1.3.0">
    
    
    <link rel="preload" href="/wrong_question_static_page/assets/css/0.styles.f30cb765.css" as="style"><link rel="preload" href="/wrong_question_static_page/assets/js/app.9022c753.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/4.8c1e4aa5.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/5.33f70f09.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/20.082608a3.js" as="script"><link rel="prefetch" href="/wrong_question_static_page/assets/js/1.ec18c3a1.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/10.697d33d5.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/11.488795fa.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/12.12a9963e.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/13.9d026eb1.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/14.c44f0e4a.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/15.a258b8bb.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/16.92328039.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/17.c4f16e4e.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/18.4dfa396f.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/19.edba2346.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/21.5ae0ad82.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/22.89457782.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/23.53bf9e28.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/24.ae06dfc6.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/25.1d7d04a4.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/6.af07a62f.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/7.2c342931.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/8.2545b8d2.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/9.0eaa1c27.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/vuejs-paginate.c4011005.js">
    <link rel="stylesheet" href="/wrong_question_static_page/assets/css/0.styles.f30cb765.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div id="vuepress-theme-blog__global-layout"><section id="header-wrapper"><header id="header"><div class="header-wrapper"><div class="title"><a href="/wrong_question_static_page/" class="nav-link home-link">小小错题本 </a></div> <div class="header-right-wrap"><ul class="nav"><li class="nav-item"><a href="/wrong_question_static_page/tag/" class="nav-link">分类</a></li></ul> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <!----></div></div></header></section> <div id="mobile-header"><div class="mobile-header-bar"><div class="mobile-header-title"><a href="/wrong_question_static_page/" class="nav-link mobile-home-link">小小错题本 </a> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg></div> <div class="mobile-menu-wrapper"><hr class="menu-divider"> <ul class="mobile-nav"><li class="mobile-nav-item"><a href="/wrong_question_static_page/tag/" class="nav-link">分类</a></li> <li class="mobile-nav-item"><!----></li></ul></div></div></div> <div class="content-wrapper"><div id="vuepress-theme-blog__post-layout"><article itemscope="itemscope" itemtype="https://schema.org/BlogPosting" class="vuepress-blog-theme-content"><header><h1 itemprop="name headline" class="post-title">
        网络知识（2）--Nginx
      </h1> <div class="post-meta"><!----> <div class="post-meta-date"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-clock"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg> <time pubdate itemprop="datePublished" datetime="2019-10-20T13:57:38.000Z">
      Sun Oct 20 2019
    </time></div> <ul itemprop="keywords" class="post-meta-tags"><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/http" data-v-2ac33c20> http </a></li><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/前端" data-v-2ac33c20> 前端 </a></li><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/Nginx" data-v-2ac33c20> Nginx </a></li></ul></div></header> <div itemprop="articleBody" class="content__default"><h2 id="_1-nginx相关知识"><a href="#_1-nginx相关知识" class="header-anchor">#</a> 1.Nginx相关知识</h2> <h3 id="_1-1-正向代理"><a href="#_1-1-正向代理" class="header-anchor">#</a> 1.1 正向代理</h3> <blockquote><p>正向代理，意思是一个位于客户端和原始服务器(origin server)之间的服务器，为了从原始服务器取得内容，客户端向代理发送一个请求并指定目标(原始服务器)，然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。</p></blockquote> <blockquote><p>举个例子：用户想访问www.google.com，不能直接访问。需要先访问代理服务器A，A服务器会去访问www.google.com。然后将数据发送给用户。</p></blockquote> <h3 id="_1-2-反向代理"><a href="#_1-2-反向代理" class="header-anchor">#</a> 1.2 反向代理</h3> <blockquote><p>反向代理是代理服务器的一种。服务器根据客户端的请求，从其关联的一组或多组后端服务器（如Web服务器）上获取资源，然后再将这些资源返回给客户端，客户端只会得知反向代理的IP地址，而不知道在代理服务器后面的服务器簇的存在.</p></blockquote> <h3 id="_1-3-负载均衡"><a href="#_1-3-负载均衡" class="header-anchor">#</a> 1.3 负载均衡</h3> <blockquote><p>负载均衡（Load Balance）其意思就是分摊到多个操作单元(服务器)上进行执行，例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等，从而共同完成工作任务。</p></blockquote> <h3 id="_1-4-动静分离"><a href="#_1-4-动静分离" class="header-anchor">#</a> 1.4 动静分离</h3> <blockquote><p>动静分离是指在web服务器架构中，将静态页面与动态页面或者静态内容接口和动态内容接口分开不同系统访问的架构设计方法，进而提升整个服务访问性能和可维护性。</p></blockquote> <h3 id="_1-5-常用命令"><a href="#_1-5-常用命令" class="header-anchor">#</a> 1.5 常用命令</h3> <div class="language-sh extra-class"><pre class="language-sh"><code>nginx -s start <span class="token comment"># 开始</span>
nginx -s reload <span class="token comment"># 重新加载</span>
nginx -s stop <span class="token comment"># 停止</span>

</code></pre></div><h3 id="_1-6-nginx配置"><a href="#_1-6-nginx配置" class="header-anchor">#</a> 1.6 nginx配置</h3> <div class="language-sh extra-class"><pre class="language-sh"><code><span class="token comment"># =================全局模块==============================</span>
<span class="token comment">#user  nobody;</span>
worker_processes  <span class="token number">1</span><span class="token punctuation">;</span>    <span class="token comment">#并发处理的值。越大，并发量越大</span>

<span class="token comment">#error_log  logs/error.log;</span>
<span class="token comment">#error_log  logs/error.log  notice;</span>
<span class="token comment">#error_log  logs/error.log  info;</span>

<span class="token comment">#pid        logs/nginx.pid;</span>


<span class="token comment"># ========================events模块=======================</span>
<span class="token comment"># 影响nginx服务器与用户的链接</span>
events <span class="token punctuation">{</span>
    worker_connections  <span class="token number">1024</span><span class="token punctuation">;</span>   <span class="token comment"># nginx最大连接数，对性能影响较大</span>
<span class="token punctuation">}</span>

<span class="token comment">#=======================http模块============================</span>
http <span class="token punctuation">{</span>
    include       mime.types<span class="token punctuation">;</span>
    default_type  application/octet-stream<span class="token punctuation">;</span>

    <span class="token comment">#log_format  main  '$remote_addr - $remote_user [$time_local] &quot;$request&quot; '</span>
    <span class="token comment">#                  '$status $body_bytes_sent &quot;$http_referer&quot; '</span>
    <span class="token comment">#                  '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;';</span>

    <span class="token comment">#access_log  logs/access.log  main;</span>

    sendfile        on<span class="token punctuation">;</span>
    <span class="token comment">#tcp_nopush     on;</span>

    <span class="token comment">#keepalive_timeout  0;</span>
    keepalive_timeout  <span class="token number">65</span><span class="token punctuation">;</span>

    <span class="token comment">#gzip  on;</span>

    server <span class="token punctuation">{</span>
        listen       <span class="token number">9999</span><span class="token punctuation">;</span>                            <span class="token comment"># nginx监听的端口号</span>
        server_name  localhost<span class="token punctuation">;</span>                       <span class="token comment">#主机名称</span>

        <span class="token comment">#charset koi8-r;</span>

        <span class="token comment">#access_log  logs/host.access.log  main;</span>

        location / <span class="token punctuation">{</span>
            root   html<span class="token punctuation">;</span>
            index  index.html index.htm<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">#error_page  404              /404.html;</span>

        <span class="token comment"># redirect server error pages to the static page /50x.html</span>
        <span class="token comment">#</span>
        error_page   <span class="token number">500</span> <span class="token number">502</span> <span class="token number">503</span> <span class="token number">504</span>  /50x.html<span class="token punctuation">;</span>
        location <span class="token operator">=</span> /50x.html <span class="token punctuation">{</span>
            root   html<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment"># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span>
        <span class="token comment">#</span>
        <span class="token comment">#location ~ \.php$ {</span>
        <span class="token comment">#    proxy_pass   http://127.0.0.1;</span>
        <span class="token comment">#}</span>

        <span class="token comment"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>
        <span class="token comment">#</span>
        <span class="token comment">#location ~ \.php$ {</span>
        <span class="token comment">#    root           html;</span>
        <span class="token comment">#    fastcgi_pass   127.0.0.1:9000;</span>
        <span class="token comment">#    fastcgi_index  index.php;</span>
        <span class="token comment">#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span>
        <span class="token comment">#    include        fastcgi_params;</span>
        <span class="token comment">#}</span>

        <span class="token comment"># deny access to .htaccess files, if Apache's document root</span>
        <span class="token comment"># concurs with nginx's one</span>
        <span class="token comment">#</span>
        <span class="token comment">#location ~ /\.ht {</span>
        <span class="token comment">#    deny  all;</span>
        <span class="token comment">#}</span>
    <span class="token punctuation">}</span>


    <span class="token comment"># another virtual host using mix of IP-, name-, and port-based configuration</span>
    <span class="token comment">#</span>
    <span class="token comment">#server {</span>
    <span class="token comment">#    listen       8000;</span>
    <span class="token comment">#    listen       somename:8080;</span>
    <span class="token comment">#    server_name  somename  alias  another.alias;</span>

    <span class="token comment">#    location / {</span>
    <span class="token comment">#        root   html;</span>
    <span class="token comment">#        index  index.html index.htm;</span>
    <span class="token comment">#    }</span>
    <span class="token comment">#}</span>


    <span class="token comment"># HTTPS server</span>
    <span class="token comment">#</span>
    <span class="token comment">#server {</span>
    <span class="token comment">#    listen       443 ssl;</span>
    <span class="token comment">#    server_name  localhost;</span>

    <span class="token comment">#    ssl_certificate      cert.pem;</span>
    <span class="token comment">#    ssl_certificate_key  cert.key;</span>

    <span class="token comment">#    ssl_session_cache    shared:SSL:1m;</span>
    <span class="token comment">#    ssl_session_timeout  5m;</span>

    <span class="token comment">#    ssl_ciphers  HIGH:!aNULL:!MD5;</span>
    <span class="token comment">#    ssl_prefer_server_ciphers  on;</span>

    <span class="token comment">#    location / {</span>
    <span class="token comment">#        root   html;</span>
    <span class="token comment">#        index  index.html index.htm;</span>
    <span class="token comment">#    }</span>
    <span class="token comment">#}</span>

<span class="token punctuation">}</span>

</code></pre></div><h2 id="_2-nginx配置实例"><a href="#_2-nginx配置实例" class="header-anchor">#</a> 2.nginx配置实例</h2> <h3 id="_2-1-反向代理1"><a href="#_2-1-反向代理1" class="header-anchor">#</a> 2.1 反向代理1</h3> <ul><li>实现效果：打开浏览器，地址栏输入 www.123.com(http://192.168.17.129:80)，跳转到linux系统Tomact主界面(http://127.0.0.1:8080)</li></ul> <div class="language-sh extra-class"><pre class="language-sh"><code>
    server <span class="token punctuation">{</span>
        listen       <span class="token number">80</span><span class="token punctuation">;</span>
        server_name  <span class="token number">192.168</span>.17.129<span class="token punctuation">;</span>

        <span class="token comment">#charset koi8-r;</span>

        <span class="token comment">#access_log  logs/host.access.log  main;</span>

        location / <span class="token punctuation">{</span>
            root   html<span class="token punctuation">;</span>
            proxy_pass http://127.0.0.1:8080
            index  index.html index.htm<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
</code></pre></div><h3 id="_2-2-反向代理2"><a href="#_2-2-反向代理2" class="header-anchor">#</a> 2.2 反向代理2</h3> <ul><li>实现效果：使用nginx反向代理，根据访问的路径跳转到不同端口的服务中。nginx监听端口为9001</li> <li>访问http://127.0.0.1:9001/edu 直接跳转到 http://127.0.0.1:8080</li> <li>访问http://127.0.0.1:9001/vod 直接跳转到 http://127.0.0.1:8081</li></ul> <div class="language-sh extra-class"><pre class="language-sh"><code>    server <span class="token punctuation">{</span>
       listen       <span class="token number">9001</span><span class="token punctuation">;</span>
       server_name       <span class="token number">192.168</span>.17.129<span class="token punctuation">;</span>

       location ~ /edu/ <span class="token punctuation">{</span>                      <span class="token comment"># ~ 表示后面是正则匹配</span>
           proxy_pass http://127.0.0.1:8080
       <span class="token punctuation">}</span>
       location ~ /vod/ <span class="token punctuation">{</span>
           proxy_pass http://127.0.0.1:8081
       <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre></div><h3 id="_2-3-location-配置说明"><a href="#_2-3-location-配置说明" class="header-anchor">#</a> 2.3 location 配置说明</h3> <ol><li>= ：用于不含正则表达式的uri前，要求请求字符与uri严格匹配</li> <li>~ ：用于表示uri包含正则表达式，区分大小写</li> <li>~* ：用于表示uri包含正则表达式，不区分大小写</li> <li>^~ : 用于不含正则表达式的uri前，要求nginx服务器找到标识uri和请求字符串匹配最高的location后，立即使用此location处理清除，而不再使用location块中的正则uri和请求字符串做匹配。
注意：如果uri包含正则表达式，则必须要有~ 或者~*标识。</li></ol> <h3 id="_2-4-负载均衡"><a href="#_2-4-负载均衡" class="header-anchor">#</a> 2.4 负载均衡</h3> <div class="language-sh extra-class"><pre class="language-sh"><code>    <span class="token comment"># 负载均衡服务器</span>
    upstream myserver <span class="token punctuation">{</span>
        server <span class="token number">192.168</span>.17.129:8080<span class="token punctuation">;</span>
        server <span class="token number">192.168</span>.17.129:8081<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    server <span class="token punctuation">{</span>
        listen       <span class="token number">80</span><span class="token punctuation">;</span>
        server_name  <span class="token number">192.168</span>.17.129<span class="token punctuation">;</span>

        <span class="token comment">#charset koi8-r;</span>

        <span class="token comment">#access_log  logs/host.access.log  main;</span>

        location / <span class="token punctuation">{</span>
            proxy_pass http://myserver<span class="token punctuation">;</span>  <span class="token comment"># 将请求转发给负载均衡服务器</span>
            root   html<span class="token punctuation">;</span>
            index  index.html index.htm<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
</code></pre></div><h4 id="_2-4-1-负载均衡策略"><a href="#_2-4-1-负载均衡策略" class="header-anchor">#</a> 2.4.1 负载均衡策略</h4> <ul><li>轮询 （默认）  每个请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器down掉，能自动剔除</li> <li>weight  权重策略</li></ul> <div class="language-sh extra-class"><pre class="language-sh"><code> upstream myserver <span class="token punctuation">{</span>
      server <span class="token number">192.168</span>.17.129:8080 <span class="token assign-left variable">weight</span><span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">;</span>
      server <span class="token number">192.168</span>.17.129:8081 <span class="token assign-left variable">weight</span><span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
</code></pre></div><ul><li>ip_hash : 后台根据访问的ip固定后台响应的服务器，一一对应起来</li></ul> <div class="language-sh extra-class"><pre class="language-sh"><code> upstream myserver <span class="token punctuation">{</span>
      ip_hash<span class="token punctuation">;</span>
      server <span class="token number">192.168</span>.17.129:8080 <span class="token assign-left variable">weight</span><span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">;</span>
      server <span class="token number">192.168</span>.17.129:8081 <span class="token assign-left variable">weight</span><span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
</code></pre></div><ul><li>fair  根据后台的服务器响应时间来分配请求，响应时间短的优先分配。</li></ul> <div class="language-sh extra-class"><pre class="language-sh"><code> upstream myserver <span class="token punctuation">{</span>
      fair<span class="token punctuation">;</span>
      server <span class="token number">192.168</span>.17.129:8080 <span class="token assign-left variable">weight</span><span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">;</span>
      server <span class="token number">192.168</span>.17.129:8081 <span class="token assign-left variable">weight</span><span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
</code></pre></div></div> <footer><!----> <hr> <!----></footer></article> <div class="sticker vuepress-toc"><div class="vuepress-toc-item vuepress-toc-h2 active"><a href="#_1-nginx相关知识" title="1.Nginx相关知识">1.Nginx相关知识</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_1-1-正向代理" title="1.1 正向代理">1.1 正向代理</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_1-2-反向代理" title="1.2 反向代理">1.2 反向代理</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_1-3-负载均衡" title="1.3 负载均衡">1.3 负载均衡</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_1-4-动静分离" title="1.4 动静分离">1.4 动静分离</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_1-5-常用命令" title="1.5 常用命令">1.5 常用命令</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_1-6-nginx配置" title="1.6 nginx配置">1.6 nginx配置</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#_2-nginx配置实例" title="2.nginx配置实例">2.nginx配置实例</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_2-1-反向代理1" title="2.1 反向代理1">2.1 反向代理1</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_2-2-反向代理2" title="2.2 反向代理2">2.2 反向代理2</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_2-3-location-配置说明" title="2.3 location 配置说明">2.3 location 配置说明</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_2-4-负载均衡" title="2.4 负载均衡">2.4 负载均衡</a></div></div></div></div> <footer class="footer" data-v-06e1b74b><div class="footer-left-wrap" data-v-06e1b74b><ul class="contact" data-v-06e1b74b></ul></div> <div class="footer-right-wrap" data-v-06e1b74b><ul class="copyright" data-v-06e1b74b></ul></div></footer></div><div class="global-ui"><!----></div></div>
    <script src="/wrong_question_static_page/assets/js/app.9022c753.js" defer></script><script src="/wrong_question_static_page/assets/js/4.8c1e4aa5.js" defer></script><script src="/wrong_question_static_page/assets/js/5.33f70f09.js" defer></script><script src="/wrong_question_static_page/assets/js/20.082608a3.js" defer></script>
  </body>
</html>
